const axios = require('axios')
const { Message } = require('element-ui')


module.exports = {
    namespaced: true,
    state:{
        allGoods:[],
        goodsDate:[],
        pageData : {
            pageSize: 4,
            currentPage: 1,
            total: 0,
            rows: [],
        },
        pageData2: {
            pageSize: 3,
            currentPage: 1,
        },
        pageData1: {
            pageSize: 3,
            currentPage: 1,
        },
        pageData3: {
            pageSize: 3,
            currentPage: 1,
        },
        allShops: [],
        unsoldGoods: [],
        unsoldGoods1: []
    },
    mutations: {
        /**putonGoodsCellAsync
        * =============================李兴乾====================
        */
        //改变pageData中的数组和总数
        getGoods(state, data) {
            console.log(data)
            state.allGoods = data
            console.log(state.allGoods )
        },
        goodsECharts(state, data) {
            state.goodsDate = data
            state.pageData.total = data.total;
            state.pageData.rows = data.rows
        },
        //分页操作
        changepageSize(state,size){
            state.pageData.pageSize = size;
            // console.log(state.pageData.pageSize);
        },
        changePage(state,page){
            state.pageData.currentPage = page
            // console.log(page);
        // //分页操作
        // changepageSize(state, size) {
        //     state.pageData.pageSize = size;
        //     // console.log(state.pageData.pageSize);

        // },
        // changePage(state, page) {
        //     state.pageData.currentPage = page
        //     // console.log(page);

        // },
        },



        /**
        * =============================李兴乾====================
        */

        /**
         * 
         *======================李焰============================
         */

        //获取未上架商品
        getUnsoldGoods(state, data) {
            state.unsoldGoods = data
        },
        //获取未上架商品
        getUnsoldGoods1(state, data) {
            state.unsoldGoods1 = data
        },

        /**
         * 
         *======================李焰============================
         */




        allShops(state, shops) {
            state.allShops = [...shops]
        }
    },
    actions: {
        /**
         * ========================================李兴乾===============================
         */
        //分页获取上架商品
        async getGoodsCellAsync(context, searchData) {
            // 
            let fy = {
                pageSize: context.state.pageData3.pageSize,
                currentPage: context.state.pageData3.currentPage,
                ...searchData,
                state: "全部"
            }
            const { data } = await axios.get("/goods/getGoodsSell", { params: fy });
            console.log(data);
            //使用mutations函数
            const good = data.rows
            context.state.pageData3.total = data.total;
            context.commit('getGoods', [...good]);
        },

        //下架商品 '/goods/deleteGoodsSell'
        async deleteGoodsCellAsync(context, id) {
            const { data } = await axios.get("/goods/deleteGoodsSell", { params: { _id: id } });
            // console.log(data); 
            context.dispatch('getGoodsCellAsync')
        },
        //修改上架商品/goods/updateGoodsSell
        async updataGoodsCellAsync(context, row) {
            const { data } = await axios.post("/goods/updateGoodsSell", row);
            // console.log(data);
            context.dispatch('getGoodsCellAsync')
        },
        //库存里面删除商品
        async deleteGoods(context, _id) {
            const { data } = await axios.post("/goods/deleteGoods", { _id });
            // console.log(data);
            if (data) {
                Message({
                    type: "success",
                    message: "删除成功！"
                });
            }
            context.dispatch('getGoodsCellAsync')
         },
         //获取统计总数
         async getgoodsecharts(context) {
            const { data } = await axios.get("/goods/getgoodsECharts");
            // console.info(data)
            context.commit('goodsECharts',data)
          },
         //获取所有的门店
         async getShops(context,){
            const { data } = await axios.get("/shops/getShops");
            // console.log(data)
            context.commit("allShops", data.rows)
        }
        ,


        /**
         * 
         * ========================李焰=============================
         */
        //上架商品
        async putonGoodsCellAsync(context, id) {
            // alert("111")
            const { data } = await axios.post("/goods/puton", { _id: id });
            // console.log(data); 
            context.dispatch('getGoodsCellAsync')
            context.dispatch('getGoodsunsoldAsync')
            context.dispatch('getGoodsunsoldAsync1')
        },
        //获取库存的商品
        async getGoodsunsoldAsync(context, searchData) {
            let fy = {
                pageSize: context.state.pageData1.pageSize,
                currentPage: context.state.pageData1.currentPage,
                ...searchData,
                state: "库存"
            }
            const { data } = await axios.get("/goods/getGoodsSell", { params: fy });
            const good = data.rows
            // console.log(data)
            context.state.pageData1.total = data.total;
            context.commit('getUnsoldGoods', [...good]);
        },
        //获取下架的商品
        async getGoodsunsoldAsync1(context, searchData) {
            let fy = {
                pageSize: context.state.pageData2.pageSize,
                currentPage: context.state.pageData2.currentPage,
                ...searchData,
                state: "下架"
            }
            const { data } = await axios.get("/goods/getGoodsSell", { params: fy });
            const good = data.rows
            context.state.pageData2.total = data.total;
            context.commit('getUnsoldGoods1', [...good]);
        },





        

        handleCurrentChange(context, val) {
            context.state.pageData.currentPage = val
            context.dispatch('getGoodsCellAsync')
            context.dispatch('getGoodsunsoldAsync')
            context.dispatch('getGoodsunsoldAsync1')

        },
        handleSizeChange(context, val) {
            context.state.pageData.pageSize = val
            context.dispatch('getGoodsCellAsync')
            context.dispatch('getGoodsunsoldAsync')
            context.dispatch('getGoodsunsoldAsync1')
        },
        handleCurrentChange1(context, val) {
            console.log(val)
            context.state.pageData1.currentPage = val
            console.log(context.state.pageData1)
            context.dispatch('getGoodsCellAsync')
            context.dispatch('getGoodsunsoldAsync')
            context.dispatch('getGoodsunsoldAsync1')
        },
        handleSizeChange1(context, val) {
            context.state.pageData1.pageSize = val
            context.dispatch('getGoodsCellAsync')
            context.dispatch('getGoodsunsoldAsync')
            context.dispatch('getGoodsunsoldAsync1')
        },
        handleCurrentChange2(context, val) {
            context.state.pageData2.currentPage = val
            context.dispatch('getGoodsCellAsync')
            context.dispatch('getGoodsunsoldAsync')
            context.dispatch('getGoodsunsoldAsync1')
        },
        handleSizeChange2(context, val) {
            context.state.pageData2.pageSize = val
            context.dispatch('getGoodsCellAsync')
            context.dispatch('getGoodsunsoldAsync')
            context.dispatch('getGoodsunsoldAsync1')
        },
        handleCurrentChange3(context, val) {
            console.log(val)
            context.state.pageData3.currentPage = val
            context.dispatch('getGoodsCellAsync')
            context.dispatch('getGoodsunsoldAsync')
            context.dispatch('getGoodsunsoldAsync1')
        },
        handleSizeChange3(context, val) {
            context.state.pageData3.pageSize = val
            context.dispatch('getGoodsCellAsync')
            context.dispatch('getGoodsunsoldAsync')
            context.dispatch('getGoodsunsoldAsync1')
        },

    }







}